package org.bronycorps.mlptss.domain.model.script;

import org.bronycorps.mlptss.domain.actor.AbstractActor;
import org.bronycorps.mlptss.domain.actor.SpecialMissileActor;
import org.bronycorps.mlptss.domain.ai.actor.VirtualActor;
import org.bronycorps.mlptss.domain.ai.actor.VirtualIndestructibleActor;
import org.bronycorps.mlptss.domain.controller.SpecialMissileController;
import org.bronycorps.mlptss.domain.layer.MissileLayer;
import org.bronycorps.mlptss.domain.layer.Track;
import org.bronycorps.mlptss.domain.model.Player;
import org.bronycorps.mlptss.domain.model.SpecialMissile;
import org.bronycorps.mlptss.manager.TrajectFactory;

import aurelienribon.tweenengine.BaseTween;
import aurelienribon.tweenengine.Timeline;
import aurelienribon.tweenengine.TweenCallback;
import aurelienribon.tweenengine.TweenManager;

import com.badlogic.gdx.math.Vector2;

public class SpecialMissileScriptMediator {

	private int count;
	private Track destTrack;
	private MissileLayer missileLayer;
	private Track sourceTrack;

	public SpecialMissileScriptMediator(MissileLayer missileLayer,
			Track sourceTrack, Track destTrack, int count) {
		super();
		this.missileLayer = missileLayer;
		this.sourceTrack = sourceTrack;
		this.destTrack = destTrack;
		this.count = count;
	}

	public SpecialMissileActor createSpecialMissileActor() {

		SpecialMissile model = new SpecialMissile((Player) this.sourceTrack
				.getPlayer().getModel());
		SpecialMissileController controller = new SpecialMissileController(
				model);
		SpecialMissileActor actor = new SpecialMissileActor(controller,
				this.destTrack);

		this.missileLayer.addSpecialMissile(actor);

		return actor;
	}

	public VirtualActor createVirtualSpecialMissileActor(AbstractActor source) {
		return new VirtualIndestructibleActor(source);
	}

	public int getMissileCount() {
		return this.count;
	}

	public Vector2 getRandomLocalPosition() {
		float y0 = (float) (Math.random() * 200 + 50);
		float x0 = (float) (Math.random() * 800 + 100);

		final Vector2 player = this.destTrack.getPlayer().getModelPosition()
				.cpy();

		while (player.dst(x0, y0) < 50) {
			y0 = (float) (Math.random() * 200 + 50);
			x0 = (float) (Math.random() * 800 + 100);
		}

		return new Vector2(x0, y0);
	}

	public Vector2 getSourcePosition() {
		return this.sourceTrack.getPlayer().getModelPosition().cpy()
				.add(0, this.sourceTrack.getY());
	}

	public float getSourceTrackOffset() {
		return this.sourceTrack.getY();
	}

	public Vector2 getTargetPosition() {
		return this.destTrack.getPlayer().getModelPosition().cpy()
				.add(0, this.destTrack.getY());
	}

	public float getTrackOffset() {
		return this.destTrack.getY();
	}

	public void tweenSpecialMissileActor(final SpecialMissileActor actor,
			Timeline tween) {

		TweenManager tweenManager = TrajectFactory.getInstance()
				.getTweenManager();

		tween.setCallback(new TweenCallback() {

			@Override
			public void onEvent(int type, BaseTween<?> source) {
				actor.getMissile().getModel().explode();
			}
		});

		tween.start(tweenManager);
	}

	public void tweenVirtualSpecialMissileActor(VirtualActor actor,
			Timeline tween) {
		if (this.destTrack.getPredictionManager() != null) {
			this.destTrack.getPredictionManager().addSpecialMissile(actor,
					tween);
			this.destTrack.clearAISnapshots();
		}
	}

}
